---
title: "R Figure Replication Code- Information Valence and Evaluations of Congress and Individual Legislators"
author: "H. Benjamin Ashton and B. Kal Munis"
date: "5/11/2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r libraries}


library(foreign)
library(ggplot2)
library(tidyr)
library(dplyr)
library(gridExtra)
library(ggpubr)
```

###marginal effects plots by hand (NO bots)

```{r study 1B (Fig. 1)}

AME <- c(-0.45, -1.76, -.23)

SE <- c(0.26, 0.28, 0.27)

lower <- c(-1.12, -2.52,  -1.22)

upper <- c(0.22, -1.01, 0.77)

lab <- c("Non-Partisan", "Mismatch", "Match")

oneb <- NULL
oneb$AME <- AME
oneb$SE <- SE
oneb$upper <- upper
oneb$lower <- lower
oneb$lab <- lab


oneb <- as.data.frame(oneb)
oneb$lab <- as.character(oneb$lab)

fig1b <- ggplot(oneb, aes(lab, AME)) + 
  geom_hline(yintercept=0, lty=2, lwd=1, colour="grey50") + 
geom_errorbar(aes(ymin=lower, ymax=upper), lwd=2.5, colour="dodgerblue", width=0) +
geom_point(size=4, pch=21, fill="white") + xlab("Partisanship of Congress vis a vis Respondent") +
  ylab("Marginal Effects (Negative Valence on Time Until Change)") +  scale_x_discrete(limits=c("Non-Partisan","Mismatch","Match")) + 
  theme_bw() 

```

```{r study 2A (Fig. 2)}

AME <- c(1.18, -0.29, 1.63)
lower <- c(0.39, -1.07, 0.81)
upper <- c(1.97, 0.5, 2.46)


lab <- c("Non-partisan", "Mismatch", "Match")

twoa <- NULL
twoa$AME <- AME
twoa$SE <- SE
twoa$upper <- upper
twoa$lower <- lower
twoa$lab <- lab


twoa <- as.data.frame(twoa)
twoa$lab <- as.character(twoa$lab)

fig2A <- ggplot(twoa, aes(lab, AME)) + geom_hline(yintercept=0, lty=2, lwd=1, colour="grey50") +
geom_errorbar(aes(ymin=lower, ymax=upper), lwd=2.5, colour="dodgerblue", width=0) + ylab("Marginal Effects of Negative Valence on # of Behaviors") + geom_point(size=4, pch=21, fill="white") + xlab("Partisanship of Legislator vis a vis Respondent") +
  scale_x_discrete(limits=c("Non-partisan","Mismatch","Match")) + 
  theme_bw() 

```

```{r study2B (Figure 3)}

AME <- c(0.28, -1.41, 0.73)
lower <- c(-0.49,  -2.47, -0.49)
upper <- c(1.05, -0.35,  1.96)


lab <- c("Non-partisan", "Mismatch", "Match")

twob <- NULL
twob$AME <- AME
twob$SE <- SE
twob$upper <- upper
twob$lower <- lower
twob$lab <- lab


twob <- as.data.frame(twob)
twob$lab <- as.character(twob$lab)

fig2b <- ggplot(twob, aes(lab, AME)) + geom_hline(yintercept=0, lty=2, lwd=1, colour="grey50") +
geom_errorbar(aes(ymin=lower, ymax=upper), lwd=2.5, colour="dodgerblue", width=0) + ylab("Marginal Effects of Negative Valence on # of Behaviors") + geom_point(size=4, pch=21, fill="white") + xlab("Partisanship of Legislator vis a vis Respondent") +
  scale_x_discrete(limits=c("Non-partisan","Mismatch","Match")) + 
  theme_bw() 
```

###3A/3B bars

```{r study 3A (Fig. 4)}
library(ggpubr)
##3A

AME <- c(2.98, 2.86, 2.62)

lower <- c(2.79, 2.77,  2.46)

upper <- c(3.17, 2.95, 2.78)

Valence <- c("Positive", "Neutral", "Negative")

threea <- NULL
threea$AME <- AME
threea$upper <- upper
threea$lower <- lower
threea$lab <- lab

threea <- as.data.frame(threea)
threea$lab <- as.character(threea$lab)

fig3a <- ggplot(threea) +
  geom_bar( aes(x=Valence, y=AME), stat="identity", fill="dodgerblue", alpha=0.7) + scale_x_discrete(limits=c("Positive","Neutral","Negative")) + geom_pointrange( aes(x=c(1,2,3), y=AME, ymin=lower, ymax=upper), colour="orange", alpha=0.9, size=0.6) + ylim(0, 3.2) + ylab("Mean Evaluation of Effectiveness") + ggtitle("Study 3A") + theme_bw() 
```

```{r Study 3B (Fig. 4)}
##3B

AME <- c(2.46, 2.49, 2.16)

lower <- c(2.21, 2.38,  1.93)

upper <- c(2.71, 2.61, 2.39)

Valence <- c("Positive", "Neutral", "Negative")

threeb <- NULL
threeb$AME <- AME
threeb$upper <- upper
threeb$lower <- lower
threeb$lab <- lab

threeb <- as.data.frame(threeb)
threeb$lab <- as.character(threeb$lab)

fig3b <- ggplot(threeb) +
  geom_bar( aes(x=Valence, y=AME), stat="identity", fill="dodgerblue", alpha=0.7) + scale_x_discrete(limits=c("Positive","Neutral","Negative")) + geom_pointrange( aes(x=c(1,2,3), y=AME, ymin=lower, ymax=upper), colour="orange", alpha=0.9, size=0.6) + ylim(0, 3) + ylab("Mean Evaluation of Effectiveness") + ggtitle("Study 3B") + theme_bw() 


```


###marginal effects plots with bots

```{r appendix figure A2 (Study 1B)}

AME <- c(-1.737302, -.4493927, -.0444444)

SE <- c(.376463, .3295674, .4902373)

lower <- c(-2.479559, -1.099188,  -1.011026)

lower2 <- lower-SE

upper <- c(-.9950443, .2004024, .9221372)

upper2 <- upper+SE

lab <- c("Opp. Party", "Non-partisan", "Party Match")

oneb <- NULL
oneb$AME <- AME
oneb$SE <- SE
oneb$upper <- upper
oneb$upper2 <- upper2
oneb$lower <- lower
oneb$lower2 <- lower2
oneb$lab <- lab


oneb <- as.data.frame(oneb)
oneb$lab <- as.character(oneb$lab)

FigA2_1b <- ggplot(oneb, aes(lab, AME)) + 
  geom_hline(yintercept=0, lty=2, lwd=1, colour="grey50") + geom_errorbar(aes(ymin=lower2, ymax=upper2), lwd=1, colour="lightskyblue1", width=0) + geom_errorbar(aes(ymin=lower, ymax=upper), lwd=2.5, colour="dodgerblue", width=0) +
geom_point(size=4, pch=21, fill="white") + xlab("Partisanship of Congress vis a vis Respondent") +
  ylab("Marg. Effects (Negative Valence on Time Until Change)") +  scale_x_discrete(limits=c("Opp. Party","Non-partisan","Party Match")) + 
  theme_bw() 



```


```{r Appendix figure A3- Study 2B}

AME <- c( -1.47386, .1796587, .8240166)

SE <- c(.5111181, .3795412, .5971429)

lower <- c(-2.481496,  -.5685819, -.3532116)

lower2 <- lower-SE

upper <- c( -.4662239, .9278993,  2.001245)

upper2 <- upper+SE

lab <- c("Opp. Party", "Non-partisan", "Party Match")

twob <- NULL
twob$AME <- AME
twob$SE <- SE
twob$upper <- upper
twob$upper2 <- upper2
twob$lower <- lower
twob$lower2 <- lower2
twob$lab <- lab


twob <- as.data.frame(twob)
twob$lab <- as.character(twob$lab)

figA3_2b <- ggplot(twob, aes(lab, AME)) + 
  geom_hline(yintercept=0, lty=2, lwd=1, colour="grey50") + geom_errorbar(aes(ymin=lower2, ymax=upper2), lwd=1, colour="lightskyblue1", width=0) + geom_errorbar(aes(ymin=lower, ymax=upper), lwd=2.5, colour="dodgerblue", width=0) +
geom_point(size=4, pch=21, fill="white") + xlab("Partisanship of Legislator vis a vis Respondent") +
  ylab("Marginal Effects of Neg. Valence on # of Behaviors") +  scale_x_discrete(limits=c("Opp. Party","Non-partisan","Party Match")) + 
  theme_bw() 



```


